<template>
  <el-form :model="state.form" :inline="true" class="my-form">
    <el-form-item label="编号">
      <el-input clearable v-model="state.form.work_no" style="width: 130px;" placeholder="请输入编号" />
    </el-form-item>
    <el-form-item label="商家名称">
      <el-select clearable v-model="state.form.client_no" placeholder="请选择商家" style="width: 130px;">
        <el-option :label="item.short_name" :value="item.client_no" v-for="item in state.clients" :key="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="提交时间">
      <el-date-picker v-model="state.selectData" :editable="false" start-placeholder="结束时间" end-placeholder="结束时间"
        type="daterange" range-separator="至" value-format="YYYY-MM-DD" />
    </el-form-item>
    <el-form-item label="问题类型">
      <el-select clearable v-model="state.form.type" placeholder="全部" style="width: 100px;">
        <el-option :label="item.cn_name" :value="item.id" v-for="item in state.type" :key="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="处理状态">
      <el-select clearable v-model="state.form.status" placeholder="全部" style="width: 90px;">
        <el-option :label="item.cn_name" :value="item.id" v-for="item in state.status" :key="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit" :loading="isLoading" icon="Search">搜索</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, onMounted, watchEffect } from 'vue'
import { getAfterSaleStatus, getClients } from '@/api/operateManage/AfterSalesWorkOrder';
import { useCommonStore } from '@/stores';
import { isEmpty } from 'lodash-es';

const commonStore = useCommonStore();
const props = defineProps({
  isLoading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['search'])

const state = reactive({
  form: {
    work_no: '',
    client_no: '',
    start_time: '',
    end_time: '',
    type: '',
    status: ''
  },
  status: [],
  type: [],
  clients: [],
  selectData: ''
})


watchEffect(() => {
  if (state.selectData) {
    state.form.start_time = state.selectData[0]
    state.form.end_time = state.selectData[1]
  } else {
    state.form.start_time = ''
    state.form.end_time = ''
  }
})

// methods
const onSubmit = () => {
  emit('search', state.form)
}
onMounted(async () => {
  try {
    if (!isEmpty(commonStore.workorderStatusEnum)) {
      state.status = commonStore.workorderStatusEnum.status;
      state.type = commonStore.workorderStatusEnum.type;
    } else {
      const res = await getAfterSaleStatus()
      if (res.code === 1000) {
        state.status = res.data.status
        state.type = res.data.type
        commonStore.workorderStatusEnum = res.data
      }
    }

  } catch (error) {
    console.log(error);
  }


  if (!isEmpty(commonStore.merchantList)) {
    state.clients = commonStore.merchantList;
    return;
  }
  const clients = await getClients()
  if (clients.code === 1000) {
    state.clients = clients.data;
    commonStore.merchantList = clients.data;
  }
})
</script>

<style lang="less" scoped>
.my-form {
  padding: var(--el-main-padding);
  font-size: 16px;

  .text-center {
    text-align: center;
  }

  :deep(.el-form-item) {
    margin-right: 25px;
  }
}
</style>